package org.csu.mypetstorecmsapi.controller;

import org.csu.mypetstorecmsapi.common.BaseResponse;
import org.csu.mypetstorecmsapi.util.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/images")
    public BaseResponse<Object> uploadImage(MultipartFile file) throws Exception {
        //获取文件名称
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = null;
        if (originalFilename != null) {
            filename = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return BaseResponse.createForSuccess(url);
    }
}
